package com.bianys.tiwen.utils;

import com.bianys.tiwen.entity.Student;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.Connection.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//此程序已经完成自动登录，获取id，提交体温。
public class TiwenUtil {

    public static boolean tianTiWen(String account,String password) {
        //调用填体温函数
        Map<String,String> cookies;
        cookies=login(account,password);
        String id=getid(cookies);
        String stuid=getstuId(cookies,id);
        String qid=getqId(cookies,id);
        if(!id.equals("")){
            //填体温
            tempercake(cookies,id,stuid,qid);
            System.out.println("Account:"+account+" Password:"+password+" success!");
            return true;
        }
        else{
            System.out.println("false");
            return false;
        }
    }
    //登录获取COOKIES。
    public static Map<String,String> login(String username, String password) {
        Map<String, String> Cookiesdatas = new HashMap<>();//获取到的cookies
        Connection zzconnect = Jsoup.connect("http://xscfw.hebust.edu.cn/survey/ajaxLogin");
        zzconnect.data("stuNum",username);
        zzconnect.data("pwd",password);
        try {
            Response ress=zzconnect.execute();
            Cookiesdatas= ress.cookies();
            for(String e:Cookiesdatas.keySet())
            {
//                System.out.println(e+" "+Cookiesdatas.get(e));
            }
        } catch (IOException e) {
            System.out.println("login False");
            e.printStackTrace();
        }

        return Cookiesdatas;
    }
    //获取stuId
    public static String getstuId(Map<String,String>Cookiesdatas,String id)
    {
        try {
            Response ress=Jsoup.connect("http://xscfw.hebust.edu.cn/survey/surveyEdit?id="+id)
                    .cookies(Cookiesdatas)
                    .execute();
            Document DocFindId=ress.parse();
            Elements ssid=DocFindId.select("input");
            String thisid=ssid.get(1).attr("value");
            //System.out.println("stuid="+thisid);
            return thisid;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    //获取id
    public static String getid(Map<String,String>Cookiesdatas)
    {
        Connection idconnect=Jsoup.connect("http://xscfw.hebust.edu.cn/survey/index").cookies(Cookiesdatas);
        try {
            Response idress= idconnect.execute();
            Document DocFindId=Jsoup.parse(idress.body());
            Elements id=DocFindId.select("li[sid]");
            String thisid=id.attr("sid");
            //System.out.println("id="+thisid);
            return thisid;
        } catch (IOException e) {
            System.out.println("GetId False");
            e.printStackTrace();
        }
        return "";
    }
    //获取qid
    public static String getqId(Map<String,String>Cookiesdatas,String id)
    {
        try {
            Response ress=Jsoup.connect("http://xscfw.hebust.edu.cn/survey/surveyEdit?id="+id)
                    .cookies(Cookiesdatas)
                    .execute();
            Document DocFindId=ress.parse();
            Elements ssid=DocFindId.select("input");
            String thisid= ssid.get(2).attr("value");
            //System.out.println("qid="+thisid);
            return thisid;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    //进入体温登录界面填体温并提交
    public static void tempercake(Map<String,String> cookies,String id,String stuId,String qid)
    {
        String url="http://xscfw.hebust.edu.cn/survey/surveySave?timestamp="+ System.currentTimeMillis();
        try {
            Jsoup.connect(url).cookies(cookies)
                    .data("id",id)
                    .data("stuId",stuId)
                    .data("qid",qid)
                    .data("location","")
                    .data("c0","不超过37.3℃，正常")
                    .data("c1","36.5")
                    .data("c2","不超过37.3℃，正常")
                    .data("c4","36.5")
                    .data("c6","健康")
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
